package com.lzx.adx.report.support.strategy.channel;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.baidu.BaiduRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.WPH)
public class WphReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new LinkedHashMap<>();

        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            params.put("imei_md5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            params.put("oaid_md5", reportDTO.getOaidSum());
        }
        params.put("device_type", 2);
        params.put("click_time",System.currentTimeMillis());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("click_ip",StringUtils.isNotBlank(reportDTO.getIp())?reportDTO.getIp():"");
        params.put("user_agent",StringUtils.isNotBlank(reportDTO.getUser_agent())?reportDTO.getUser_agent():"");
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback_url",callBackUrl);
        String parms = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"&"+parms;
    }
}
